<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="员工id号" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入员工id号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="员工名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入员工名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="职位" prop="post">
        <el-input
          v-model="queryParams.post"
          placeholder="请输入职位"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="入职时间">
        <el-date-picker
          v-model="daterangeEntryDate"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="离职时间">
        <el-date-picker
          v-model="daterangeResignationDate"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="身份证号" prop="idCard">
        <el-input
          v-model="queryParams.idCard"
          placeholder="请输入身份证号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="出生日期">
        <el-date-picker
          v-model="daterangeBirthday"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="年龄" prop="age">
        <el-input
          v-model="queryParams.age"
          placeholder="请输入年龄"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户性别" prop="sex">
        <el-select v-model="queryParams.sex" placeholder="请选择用户性别" clearable>
          <el-option
            v-for="dict in dict.type.sys_user_sex"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="最高学历" prop="highEducationalLevel">
        <el-select v-model="queryParams.highEducationalLevel" placeholder="请选择最高学历" clearable>
          <el-option
            v-for="dict in dict.type.education"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="手机号" prop="phoneNumber">
        <el-input
          v-model="queryParams.phoneNumber"
          placeholder="请输入手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用工类型" prop="typeOfMen">
        <el-input
          v-model="queryParams.typeOfMen"
          placeholder="请输入用工类型"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="离职原因" prop="reasonForLeaving">
        <el-input
          v-model="queryParams.reasonForLeaving"
          placeholder="请输入离职原因"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="离职情况" prop="resignationSituation">
        <el-input
          v-model="queryParams.resignationSituation"
          placeholder="请输入离职情况"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="住宿情况" prop="dormitoryNeed">
        <el-input
          v-model="queryParams.dormitoryNeed"
          placeholder="请输入住宿情况"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['resignation_record:resignation_record:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['resignation_record:resignation_record:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['resignation_record:resignation_record:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['resignation_record:resignation_record:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="resignation_recordList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="员工id号" align="center" prop="userId" />
      <el-table-column label="员工名称" align="center" prop="name" />
      <el-table-column label="部门名称" align="center" prop="deptName" />
      <el-table-column label="职位" align="center" prop="post" />
      <el-table-column label="入职时间" align="center" prop="entryDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.entryDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="离职时间" align="center" prop="resignationDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.resignationDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="身份证号" align="center" prop="idCard" />
      <el-table-column label="出生日期" align="center" prop="birthday" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.birthday, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="年龄" align="center" prop="age" />
      <el-table-column label="用户性别" align="center" prop="sex">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.sex"/>
        </template>
      </el-table-column>
      <el-table-column label="最高学历" align="center" prop="highEducationalLevel">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.education" :value="scope.row.highEducationalLevel"/>
        </template>
      </el-table-column>
      <el-table-column label="手机号" align="center" prop="phoneNumber" />
      <el-table-column label="用工类型" align="center" prop="typeOfMen"/>

      <el-table-column label="离职原因" align="center" prop="reasonForLeaving" />
      <el-table-column label="离职情况" align="center" prop="resignationSituation" />
      <el-table-column label="住宿情况" align="center" prop="dormitoryNeed" />
      <el-table-column label="离职备注" align="center" prop="notes" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['resignation_record:resignation_record:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['resignation_record:resignation_record:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改离职记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="员工id号" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入员工的id号" />
        </el-form-item>
        <el-form-item label="员工名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入员工名称" />
        </el-form-item>
        <el-form-item label="职位" prop="post">
          <el-input v-model="form.post" placeholder="请输入职位" />
        </el-form-item>
        <el-form-item label="入职时间" prop="entryDate">
          <el-date-picker clearable
            v-model="form.entryDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择入职时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="离职时间" prop="resignationDate">
          <el-date-picker clearable
            v-model="form.resignationDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择离职时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="身份证号" prop="idCard">
          <el-input v-model="form.idCard" placeholder="请输入身份证号" />
        </el-form-item>
        <el-form-item label="出生日期" prop="birthday">
          <el-date-picker clearable
            v-model="form.birthday"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择出生日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input v-model="form.age" placeholder="请输入年龄" />
        </el-form-item>
        <el-form-item label="用户性别" prop="sex">
          <el-select v-model="form.sex" placeholder="请选择用户性别">
            <el-option
              v-for="dict in dict.type.sys_user_sex"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="最高学历" prop="highEducationalLevel">
          <el-select v-model="form.highEducationalLevel" placeholder="请输入最高学历">
            <el-option
              v-for="dict in dict.type.education"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="手机号" prop="phoneNumber">
          <el-input v-model="form.phoneNumber" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="用工类型" prop="typeOfMen">
          <el-input v-model="form.typeOfMen" placeholder="请输入用工类型" />
        </el-form-item>
        <el-form-item label="离职原因" prop="reasonForLeaving">
          <el-input v-model="form.reasonForLeaving" placeholder="请输入离职原因" />
        </el-form-item>
        <el-form-item label="离职情况" prop="resignationSituation">
          <el-input v-model="form.resignationSituation" placeholder="请输入离职情况" />
        </el-form-item>
        <el-form-item label="住宿情况" prop="dormitoryNeed">
          <el-input v-model="form.dormitoryNeed" placeholder="请输入住宿情况" />
        </el-form-item>
        <el-form-item label="离职备注" prop="notes">
          <el-input v-model="form.notes" placeholder="请输入离职备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listResignation_record, getResignation_record, delResignation_record, addResignation_record, updateResignation_record } from "@/api/Empment/resignation_record";

export default {
  name: "Resignation_record",
  dicts: ['sys_user_sex','education'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 离职记录表格数据
      resignation_recordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 离职备注时间范围
      daterangeEntryDate: [],
      // 离职备注时间范围
      daterangeResignationDate: [],
      // 离职备注时间范围
      daterangeBirthday: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        name: null,
        deptName: null,
        post: null,
        entryDate: null,
        resignationDate: null,
        idCard: null,
        birthday: null,
        age: null,
        sex: null,
        highEducationalLevel: null,
        phoneNumber: null,
        typeOfMen: null,
        reasonForLeaving: null,
        resignationSituation: null,
        dormitoryNeed: null,
        notes: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userId: [
          { required: true, message: "员工的id号不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询离职记录列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeEntryDate && '' != this.daterangeEntryDate) {
        this.queryParams.params["beginEntryDate"] = this.daterangeEntryDate[0];
        this.queryParams.params["endEntryDate"] = this.daterangeEntryDate[1];
      }
      if (null != this.daterangeResignationDate && '' != this.daterangeResignationDate) {
        this.queryParams.params["beginResignationDate"] = this.daterangeResignationDate[0];
        this.queryParams.params["endResignationDate"] = this.daterangeResignationDate[1];
      }
      if (null != this.daterangeBirthday && '' != this.daterangeBirthday) {
        this.queryParams.params["beginBirthday"] = this.daterangeBirthday[0];
        this.queryParams.params["endBirthday"] = this.daterangeBirthday[1];
      }
      listResignation_record(this.queryParams).then(response => {
        this.resignation_recordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        name: null,
        deptName: null,
        post: null,
        entryDate: null,
        resignationDate: null,
        idCard: null,
        birthday: null,
        age: null,
        sex: null,
        highEducationalLevel: null,
        phoneNumber: null,
        typeOfMen: null,
        reasonForLeaving: null,
        resignationSituation: null,
        dormitoryNeed: null,
        notes: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeEntryDate = [];
      this.daterangeResignationDate = [];
      this.daterangeBirthday = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加离职记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getResignation_record(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改离职记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateResignation_record(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addResignation_record(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除离职记录编号为"' + ids + '"的数据项？').then(function() {
        return delResignation_record(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('resignation_record/resignation_record/export', {
        ...this.queryParams
      }, `resignation_record_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
